#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   DTC_test.py
@Time    :   2022/07/06 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   YangLiang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume
 
import pytest
import logging
import time
from commDriver.modbus_plc import *
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
import Set_up
from collections.abc import Iterable

sys.dont_write_bytecode = True

BSM = 0
PBSM = 1
RMS = 2
NOS = 3
RSS = 4
CanNmRepeatMessageTime = 1500  # 节点在重复报文状态(Repeat Message State)中持续的时间
CanNmTimeoutTime = 3000  # 节点进入网络模式(Network Mode)后，即开启该定时器；在准备睡眠状态(Ready Sleep State)中，该定时器溢出后，节点进入准备总线睡眠模式.节点处于网络模式(Network Mode)，成功接收或成功发送一帧网络管理报文后，需重启NM-Timeout 定时器
CanNmWaitBusSleepTime = 2000  #该定时器的作用是确保所有的节点都有时间停止其网络活动
T_START_NM_TX = 150  # 节点从进入重复报文状态(Repeat Message State)，到发出第一帧网络管理报文的最大允许时间间隔。原则上，这个时间越短越好（T_WakeUp[100ms]+T_START_NM_TX[50ms]）
T_START_App_TX = 20  # 在成功发送出第一帧网络管理报文后，节点应发送出应用报文的最大时间间隔
T_All_App_X = 1000  # 所有周期应用报文必须至少被发送一次的最大允许时间。该时间间隔从唤醒事件开始计时
CanNmImmediateNmCycleTime = 20 # 节点启动快速发送机制时，需以此参数作为网络管理报文的发送周期
CanNmMsgCycleTime = 500  # 在网络模式(Network Mode)（除去准备睡眠状态）中，若快速发送机制未被启动，节点应以此参数作为网络管理报文的发送周期
T_WakeUp = 100  # 处在Bus-Sleep Mode的节点，从唤醒事件发生，到转换到Repeat Message State的最大允许时间间隔
CanNmImmediateNmTransmissions = 10  # 当节点处于重复报文状态并启用快速发送机制时，节点使用CanNmImmediateNmCycleTime为周期发送网络管理报文的数量
AppMsgCycleTime  = 50    # ms

NMID_Min = 0x400  # 网络管理报文base ID  远程唤醒报文
NMID_Max = 0x47F  # 网络管理报文max ID  远程唤醒报文

Test_frame_NM_message_ID = 0x44C  # 远程唤醒报文
Test_frame_APP_message_ID = 0x311  #  应用报文
App_message_ID = [0x59E,0x280,0x3E0,0x640,0x650,0x311,0x3F0,0x242,0x180,0x523,0x527,0x6E7,0x620,0x570,0x4A0,0x4B3]

# class message_ID(object):
#     if Set_up.act_radar_type == 'CMRR_FL':
#        Radar_NM_message_ID =  0x448
#     elif Set_up.act_radar_type == 'CMRR_FR':
#        Radar_NM_message_ID =  0x449
#        Radar_APP_message_ID = 0x384
#     elif Set_up.act_radar_type == 'CMRR_RL':
#        Radar_NM_message_ID =  0x44A
#     else:
#        Radar_APP_message_ID = 0x385
#        Radar_NM_message_ID =  0x44C
#        Radar_APP_message_ID = 0x386

class message_ID(object):
       Radar_NM_message_ID =  0x44C
       Radar_APP_message_ID = 0x224
      #  Radar_NM_message_ID =  0x449
      #  Radar_APP_message_ID = 0x384

@allure.severity('critical')
class Test_NM(object):
    # 被动唤醒,默认发单帧,发多帧时周期为500ms
    def passive_wakeup(self,Driver,iStep,id=NMID_Min,n=5):
      with allure.step(f"Step{iStep}:清buff"): 
         Driver['CAN'].fush_q_buff()  # 清缓存
         iStep += 1
      with allure.step(f"Step{iStep}:发送唤醒帧{hex(id)}"):

         for i in range(n):
            time.sleep(0.5)
            logging.info('send wakeup frame')
            passive_frame = Driver['CAN'].send_Frame(id,[0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF],'CAN')
         iStep += 1
      return iStep
    
    # 本地唤醒,KL30(常电)
    def native_wakeup(self,Driver,Eanble,iStep):
      modbus = Modbus(Set_up.NM_modbusCOM)
      if Eanble:
         with allure.step(f"Step{iStep}:清buff"): 
            Driver['CAN'].fush_q_buff()  # 清缓存
            iStep += 1
         with allure.step(f"Step{iStep}:IGN on"):
            modbus.write_single_coil(DO_Address_single.Y6.value,0x0001)
            logging.info('Connect IGN')
            iStep += 1
      else:
         with allure.step(f"Step{iStep}:IGN OFF"):
            modbus.write_single_coil(DO_Address_single.Y6.value,0x0000)
            logging.info('Disconnect IGN')
            iStep += 1
      del modbus
      return iStep

    # 本地唤醒,UDS Request B41v 不支持
    def uds_wakeup(self,Driver,iStep,n=2):
      with allure.step(f"Step{iStep}:清buff"): 
            Driver['CAN'].fush_q_buff()  # NM清缓存
            iStep += 1
      with allure.step(f"Step{iStep}:UDS Send"):
         for i in range(n):
            time.sleep(0.5)
            Driver['CAN'].send_Frame(0x74A,[0x02,0x10,0x01,0xAA,0xAA,0xAA,0xAA,0xAA],'CANFD')
            Driver['CAN'].send_Frame(0x7df,[0x02,0x10,0x03,0xAA,0xAA,0xAA,0xAA,0xAA],'CANFD')
            logging.info('UDS Send')
            iStep += 1
      return iStep

    def network_request(self,Driver,Eanble,iStep,door=False):
      if Eanble:
         with allure.step(f"Step{iStep}:清buff"): 
            Driver['CAN'].fush_q_buff()  # 清缓存
            iStep += 1
         with allure.step(f"Step{iStep}:IGN on"):  #非断电熄火（三分钟内）+落锁信号
            Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'PEPS_0x1B1','PwrMod',1)
            time.sleep(0.5)
            Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'BCM_0x2F1','DrvrDoorLockStsSwt',0)
            Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'BCM_0x2F1','PassDoorLockStsSwt',0)
            Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'BDC_BCM_2','BCM2_St_LRDoorLock',0)
            Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'BDC_BCM_2','BCM2_St_RRDoorLock',0)
            logging.info('Network Request')
            iStep += 1
      else:
         with allure.step(f"Step{iStep}:清buff"): 
            Driver['CAN'].fush_q_buff()  # 清缓存
            iStep += 1
         with allure.step(f"Step{iStep}:IGN OFF"):  #断电熄火（三分钟内）+落锁信号
            Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'PEPS_0x1B1','PwrMod',0)

            time.sleep(0.5)
            Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'BCM_0x2F1','BCM1_St_LFDoorLock',0)
            Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'BCM_0x2F1','BCM1_St_PasDoorLock',0)
            Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'BDC_BCM_2','BCM2_St_LRDoorLock',0)
            if not door:
               Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'BDC_BCM_2','BCM2_St_RRDoorLock',0)
               logging.info('Network Release')
            else:
               Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'BDC_BCM_2','BCM2_St_RRDoorLock',1)
               logging.info('Network Release')
               time.sleep(0.5)
               time.sleep(180)  # 时间待确认
               logging.info('Delay 3mins for Network Release')
            iStep += 1
      return iStep

    def get_ATimeUs(self,Driver,iStep,NM_id,APP_id,WAKEUP_id):
      with allure.step(f"Step{iStep}:f筛选分别只有{hex(NM_id)},{hex(APP_id)},{hex(WAKEUP_id)}报文的帧数据"):
         ALl_data, buffer_size = Driver['CAN'].read_frame()  # 这一时间段的数据
         NM_Data = ALl_data[ALl_data['AIdentifier']==NM_id]
         APP_Data = ALl_data[ALl_data['AIdentifier']==APP_id]
         Passive_wakeup_Data = ALl_data[ALl_data['AIdentifier']==WAKEUP_id]
         iStep += 1
      with allure.step(f"Step{iStep}:{hex(NM_id)},{hex(APP_id)}报文的时间戳"):
         NM_time_list= NM_Data['ATimeUs'].tolist()
         APP_time_list= APP_Data['ATimeUs'].tolist()
         # PASSIVE_time_list= WAKEUP_Data['ATimeUs'].tolist()
         iStep += 1
      return iStep,NM_time_list,APP_time_list,Passive_wakeup_Data,NM_Data,APP_Data

    def time_diff(self,time_list):
      if len(time_list) >= 3:
         frame3 = time_list[2]-time_list[1]
         frame2 = time_list[1]-time_list[0]
         frame0 = time_list[0]
      else:   #NM数量小于3的情况（500ms正常发送都是小于4）
         frame3 = 0
         frame2 = time_list[1]-time_list[0]
         frame0 = time_list[0]
      return frame3,frame2,frame0

    def message_format(self,Data_after_filter_Id,iStep,i=0):
      # i:筛选ID后第几帧报文(唤醒后雷达应先发NM报文 后发应用报文)
      # Source Node Identifier
      frame_info = Data_after_filter_Id.iloc[i]
      SNI = frame_info.ADatas[0]  # radar_NM_ATimeUs.iloc[0]：第一帧的所有信息
      with allure.step(f"Step{iStep}:check Source Node Identifier value is {hex(SNI)}#expected:{hex(message_ID.Radar_NM_message_ID - NMID_Min)}"):        
         SIN_expect = message_ID.Radar_NM_message_ID - NMID_Min
         # if SNI != SIN_expect:
         #    logging.ERROR(f'expect_value:{hex(SIN_expect)},actual_value:{hex(SNI)}')
         # with assume: assert SNI == SIN_expect
         logging.info(f"Source Node Identifier is {SNI}")
         iStep += 1

      # Control Bit Vector
      # frame_info = Data_after_filter_Id.iloc[i]
      with allure.step(f"Step{iStep}:获取NM_Data frame的byte1对应的bit位#expected:PNI位须一直置为1"):
         CBV = frame_info.ADatas[1]
         # with assume: assert CBV&0x40 == 1
         # logging.info(f"Partial Network Information Bit value is {CBV&0x40}")
         # iStep += 1
      with allure.step(f"Step{iStep}:NM_Data frame的byte1对于的bit1,2,3,5,7#expected:0"):
         # with assume: assert CBV&0xAE == 0
         # logging.info(f"bit1 value:{CBV&0x02},bit2 value:{CBV&0x04},bit3 value:{CBV&0x08},bit5 value:{CBV&0x20},bit7 value:{CBV&0x80}")

         Repeat_Message_Request_Bit = CBV&0x1 #  0x0：该报文发送节点未请求其他节点进入重复报文状态； 0x1：该报文发送节点请求其他节点进入重复报文状态。
         logging.info(f"Repeat Message Request Bit is {Repeat_Message_Request_Bit}")
         Active_Wakeup_Bit = CBV&0x10  # 0x00：被动唤醒 0x10：主动唤醒
         Active_Wakeup_Bit =  Active_Wakeup_Bit >> 4
         logging.info(f"Active Wakeup Bit is {Active_Wakeup_Bit}")
         iStep += 1
      with allure.step(f"Step{iStep}:获取WUS的value,当处于网络模式中时,WUS的编码值不允许变化,节点离开网络模式后,需将WUS清零"):
         WUS = frame_info.ADatas[5]
         iStep += 1
      with allure.step(f"Step{iStep}:获取NmSts的value,节点处在重复报文状态中:为0x0,节点未处在重复报文状态中:NmSts为0x1"):
         # frame_info1 = Data_after_filter_Id.iloc[1]
         User_Data_Byte1 = frame_info.ADatas[6]
         NmSts = User_Data_Byte1&0x01
         logging.info(f"NmSts is {NmSts}")
         iStep += 1
      with allure.step(f"Step{iStep}:用户数据中未使用的位应设为0x0,未使用的字节应设为0x00"):
         nouse_bit = User_Data_Byte1&0xFE
         User_Data_Byte2=frame_info.ADatas[7]
         # with assume: assert User_Data_Byte2 == nouse_bit == 0      #目前先不考虑其他byte
         logging.info(f'User_Data_Byte1 not use bit value is{User_Data_Byte1&0xFE},User_Data_Byte2 value is {User_Data_Byte2}')
         iStep += 1

      return iStep,Repeat_Message_Request_Bit,Active_Wakeup_Bit,WUS,NmSts

    def time_parameters_check(self,Driver,iStep,wakeup_mode):
      # 被动唤醒wakeup_mode=True， 主动唤醒wakeup_mode=False
      iStep,NM_time_list,APP_time_list,Passive_wakeup_Data,NM_Data,APP_Data = self.get_ATimeUs(Driver,iStep,message_ID.Radar_NM_message_ID,message_ID.Radar_APP_message_ID,NMID_Min)
      radar_NM_frame0 = NM_time_list[0]
      radar_app_frame0 = APP_time_list[0]

      with allure.step(f"Step{iStep}:判断在成功发送出第一帧网络管理报文#Expected:节点应发送出应用报文的最大时间间隔 小于 T_START_App_TX"):
         with assume: assert radar_NM_frame0 < radar_app_frame0  # check 优先发NM帧
         with assume: assert (radar_app_frame0 - radar_NM_frame0) <= ((T_START_App_TX+50)/1000)  # 在成功发送出第一帧网络管理报文后，节点应发送出应用报文的最大时间间隔
         logging.info(f'radar_app_frame0 time is {radar_app_frame0},radar_NM_frame0 time is {radar_NM_frame0}')
         iStep += 1

         # 被动唤醒
      if wakeup_mode:
         wakeup_frame0 = Passive_wakeup_Data['ATimeUs'].tolist()[0]
         with allure.step(f"Step{iStep}:判断T_START_NM_TX,T_All_App_X#Expected:T_START_NM_TX=50ms,T_All_App_X=20ms"):
            # with assume:assert (radar_NM_frame0 - wakeup_frame0) <= ((T_START_NM_TX+50)/1000)  # 从进入重复报文状态(Repeat Message State)，到发出第一帧网络管理报文的最大允许时间间隔
            # with assume:assert (radar_app_frame0 - wakeup_frame0) <= T_All_App_X
            logging.info(f'radar_app_frame0 time is {radar_app_frame0},wakeup_frame0 time is {wakeup_frame0},radar_NM_frame0 time is {radar_NM_frame0}')
         iStep += 1
      return iStep

    def BSM_mode_check(self,Driver,iStep,timeout=5):
      with allure.step(f"Step{iStep}:check 报文帧率与电流#expected:帧率=0,电流<0.001A"):
         start_time = time.time()
         while( (time.time()-start_time) < timeout):
            Radar_NM_message_fsp = Driver['CAN'].get_fps(message_ID.Radar_NM_message_ID)
            Radar_APP_message_fsp = Driver['CAN'].get_fps(message_ID.Radar_APP_message_ID)
            v,i =Driver['power'].Read_Measure()
            sleepmode = True if ((Radar_NM_message_fsp == 0 or Radar_NM_message_fsp == -1) and (Radar_APP_message_fsp == 0 or Radar_APP_message_fsp == -1)) else False   #开机fsp返回-1
            # sleepmode = True if (i < 0.08) else False
            if sleepmode:
               break
            time.sleep(0.01)
         totle_time = round(time.time() - start_time, 4)
         logging.info('total use time:{}'.format(totle_time))
         iStep += 1
         Driver['CAN'].fush_q_buff()
      return iStep,sleepmode

    # 被动唤醒wakeup_mode=1,主动唤醒wakeup_mode = 0,i:筛选ID后第几帧报文
    def RMS_mode_check(self,Driver,iStep,wakeup_mode,i=0,bit=0):
      iStep,NM_time_list,APP_time_list,Passive_wakeup_Data,NM_Data,APP_Data = self.get_ATimeUs(Driver,iStep,message_ID.Radar_NM_message_ID,message_ID.Radar_APP_message_ID,NMID_Min)

      radar_NM_frame3,radar_NM_frame2,radar_NM_frame0 = self.time_diff(NM_time_list)
      radar_app_frame3,radar_app_frame2,radar_app_frame0 = self.time_diff(APP_time_list)

      iStep,Repeat_Message_Request_Bit,Active_Wakeup_Bit,WUS,NmSts=self.message_format(NM_Data,iStep,i)
      with allure.step(f"Step{iStep}:判断Repeat_Message_Request_Bit value #Expected:1,NmSts #Expected :0x0,节点处在重复报文状态中"):
         with assume: assert Repeat_Message_Request_Bit == bit
         logging.info(f'Repeat_Message_Request_Bit value is: {Repeat_Message_Request_Bit}')
         # with assume: assert NmSts == 0
         logging.info(f'NmSts value is: {NmSts}')
         iStep += 1

      with allure.step(f"Step{iStep}:判断radar_app_frame 前两帧的周期#Expected:周期=50ms"):
         with assume: assert (AppMsgCycleTime-15)/1000 <= radar_app_frame2 <= (AppMsgCycleTime+15)/1000
         with assume: assert (AppMsgCycleTime-15)/1000 <= radar_app_frame3 <= (AppMsgCycleTime+15)/1000
         logging.info(f'radar app cycle time is{radar_app_frame2},{radar_app_frame3}')
         iStep += 1

      with allure.step(f"Step{iStep}:判断NM 帧的byte1的Active_Wakeup_Bit,判断NM 帧周期#Expected:Active_Wakeup_Bit=0,周期={CanNmMsgCycleTime}ms"):
         # 被动唤醒
         if wakeup_mode:
            with assume: assert Active_Wakeup_Bit == 0
            logging.info(f'Active_Wakeup_Bit value is: {Active_Wakeup_Bit}')
            with assume: assert (CanNmMsgCycleTime-10)/1000 <= radar_NM_frame2 <= (CanNmMsgCycleTime+10)/1000
            with assume: assert (CanNmMsgCycleTime-10)/1000 <= radar_NM_frame3 <= (CanNmMsgCycleTime+10)/1000
            logging.info(f'radar NM cycle time is{radar_NM_frame2},{radar_NM_frame3}')
         # 主动唤醒
         else:
            with assume: assert Active_Wakeup_Bit == 1
            logging.info(f'Active_Wakeup_Bit value is: {Active_Wakeup_Bit}')
            frame11_10 = NM_time_list[10]-NM_time_list[9]
            frame10_9 = NM_time_list[9]-NM_time_list[8]
            frame9_8 = NM_time_list[8]-NM_time_list[7]
            with assume: assert 0.019 < frame9_8 <= 0.020
            with assume: assert 0.019 < frame10_9 <= 0.020
            with assume: assert 0.49 < frame11_10 <= 0.51  # 快发机制发送10帧20ms的NM报文后周期在500ms，两帧之间得时间差 相差大 line 487
            logging.info(f'radar NM cycle time is{frame9_8},{frame10_9},{frame11_10}')
         iStep += 1

      return iStep

    def NOS_mode_check(self,Driver,iStep):
      Driver['CAN'].fush_q_buff()  # NM清缓存
      time.sleep(2)
      iStep,NM_time_list,APP_time_list,Passive_wakeup_Data,NM_Data,APP_Data = self.get_ATimeUs(Driver,iStep,message_ID.Radar_NM_message_ID,message_ID.Radar_APP_message_ID,NMID_Min)
      radar_NM_frame3,radar_NM_frame2,radar_NM_frame0 = self.time_diff(NM_time_list)
      radar_app_frame3,radar_app_frame2,radar_app_frame0 = self.time_diff(APP_time_list)

      with allure.step(f"Step{iStep}:判断radar_app_frame 前两帧的周期#Expected:周期=50ms"):
         with assume: assert (AppMsgCycleTime-10)/1000 <= radar_app_frame2 <= (AppMsgCycleTime+10)/1000
         with assume: assert (AppMsgCycleTime-10)/1000 <= radar_app_frame3 <= (AppMsgCycleTime+10)/1000
         logging.info(f'radar app cycle time is{radar_app_frame2},{radar_app_frame3}')
         iStep += 1
      with allure.step(f"Step{iStep}:判断NM 帧周期#Expected:周期=500ms"):
         with assume: assert (CanNmMsgCycleTime-50)/1000 <= radar_NM_frame2 <= (CanNmMsgCycleTime+50)/1000
         with assume: assert (CanNmMsgCycleTime-50)/1000 <= radar_NM_frame3 <= (CanNmMsgCycleTime+50)/1000
         logging.info(f'radar NM cycle time is{radar_NM_frame2},{radar_NM_frame3}')
         iStep += 1
      with allure.step(f"Step{iStep}:判断NM帧byte1 的bit4 Active_Wakeup_Bit#Expected: Active_Wakeup_Bit=1"):
         frame_info = NM_Data.iloc[0].ADatas[1]
         Active_Wakeup_Bit = (frame_info&0x10)>>4
         # with assume: assert Active_Wakeup_Bit == 1 # 类似0x123 唤醒 这个bit就是0，不一定都是1
         logging.info(f'Active_Wakeup_Bit value is {Active_Wakeup_Bit}')
      return iStep

    def RSS_mode_check(self,Driver,iStep):
      Driver['CAN'].fush_q_buff()  # NM清缓存
      time.sleep(2)
      iStep,NM_time_list,APP_time_list,Passive_wakeup_Data,NM_Data,APP_Data = self.get_ATimeUs(Driver,iStep,message_ID.Radar_NM_message_ID,message_ID.Radar_APP_message_ID,NMID_Min)
      radar_app_frame3,radar_app_frame2,radar_app_frame0 = self.time_diff(APP_time_list)

      with allure.step(f"Step{iStep}:判断radar_app_frame 前两帧的周期#Expected:周期=50ms"):
         with assume: assert (AppMsgCycleTime-10)/1000 <= radar_app_frame2 <= (AppMsgCycleTime+10)/1000
         with assume: assert (AppMsgCycleTime-10)/1000 <= radar_app_frame3 <= (AppMsgCycleTime+10)/1000
         logging.info(f'radar app cycle time is{radar_app_frame2},{radar_app_frame3}')
         iStep += 1

      with allure.step(f"Step{iStep}:判断radar_NM_frame帧率#Expected:帧率=0"):
         # NM_Data.values: type is numpy.ndarray
         with assume: assert NM_Data.values.any() == False  # 判断 抓取NM数据 为空(pandas.DataFrame.any())
         logging.info(f'radar NM frame info :{NM_Data.values}')
         iStep += 1
      return iStep

    def PBSM_mode_check(self,Driver,iStep):
      time.sleep(CanNmTimeoutTime/1000)  #NM-Timeout Timer expired
      with allure.step(f"Step{iStep}:判断电流#Expected:电流>80mA"):
         v,i =Driver['power'].Read_Measure()
         with assume: assert i > 0.08
         iStep += 1
      Driver['CAN'].fush_q_buff()  # NM清缓存
      time.sleep(1.5)
      with allure.step(f"Step{iStep}:判断Radar_APP_message_ID帧率#Expected:帧率=0"):
         # 改成判断 抓取APP数据 为空
         iStep,NM_time_list,APP_time_list,Passive_wakeup_Data,NM_Data,APP_Data = self.get_ATimeUs(Driver,iStep,message_ID.Radar_NM_message_ID,message_ID.Radar_APP_message_ID,NMID_Min)
         with assume: assert APP_Data.values.any() == False
         logging.info(f'radar App frame info :{APP_Data.values}')
         # Radar_APP_message_fsp = Driver['CAN'].get_fps(message_ID.Radar_APP_message_ID,'CANFD')
         # with assume: assert (Radar_APP_message_fsp == 0 or Radar_APP_message_fsp == -1)
         iStep += 1
      with allure.step(f"Step{iStep}:判断radar_NM_frame帧率#Expected:帧率=0"):
         # 改成判断 抓取NM数据 为空
         iStep,NM_time_list,APP_time_list,Passive_wakeup_Data,NM_Data,APP_Data = self.get_ATimeUs(Driver,iStep,message_ID.Radar_NM_message_ID,message_ID.Radar_APP_message_ID,NMID_Min)
         with assume: assert NM_Data.values.any() == False
         logging.info(f'radar NM frame info :{NM_Data.values}')
         # Radar_NM_message_fsp = Driver['CAN'].get_fps(message_ID.Radar_NM_message_ID)
         # with assume: assert (Radar_NM_message_fsp == 0 or Radar_NM_message_fsp == -1)
         iStep += 1
      return iStep

    @allure.severity('normal')
    @allure.description("Test Message data format by passive Start up")
    #source node identifier,unused user data byte
    def test_NM_Message_data_format_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1
      with allure.step(f"Step{iStep}:上电后等待1s,满足radar睡眠条件"):
         time.sleep(1)
         iStep += 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep)
      time.sleep(2)
      iStep,NM_time_list,APP_time_list,Passive_wakeup_Data,NM_Data,APP_Data = self.get_ATimeUs(Driver,iStep,message_ID.Radar_NM_message_ID,message_ID.Radar_APP_message_ID,NMID_Min)

      if NM_Data.empty:
         with assume:assert 0 == 1
      else:
         iStep,Repeat_Message_Request_Bit,Active_Wakeup_Bit,WUS,NmSts = self.message_format(NM_Data,iStep,0)
         with assume:assert Repeat_Message_Request_Bit == 0
         with assume:assert Active_Wakeup_Bit == 0
         with assume:assert NmSts == 0x00 #唤醒后即进入RMS mode


    @allure.severity('normal')
    @allure.description("Test can't wakeup by UDS Request Start up")
    #B41v notActive wakeup by 收到诊断报文 
    def test_uds_request_nowakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1
      with allure.step(f"Step{iStep}:上电后等待1s,满足radar睡眠条件"):
         time.sleep(1)
         iStep += 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.uds_wakeup(Driver,iStep)
      time.sleep(2)
      iStep,NM_time_list,APP_time_list,Passive_wakeup_Data,NM_Data,APP_Data = self.get_ATimeUs(Driver,iStep,message_ID.Radar_NM_message_ID,message_ID.Radar_APP_message_ID,NMID_Min)
      if not NM_Data.empty:
         with assume:assert 0 == 1
         

   #  @allure.severity('normal')
   #  @allure.description("Test Message data format by native Start up")
   #  #source node identifier,unused user data byte
   #  def test_NM_Message_data_format_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1
   #    with allure.step(f"Step{iStep}:上电后等待1s,满足radar睡眠条件"):
   #       time.sleep(1)
   #       iStep += 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    time.sleep(1.5)
   #    iStep,NM_time_list,APP_time_list,Passive_wakeup_Data,NM_Data,APP_Data = self.get_ATimeUs(Driver,iStep,message_ID.Radar_NM_message_ID,message_ID.Radar_APP_message_ID,NMID_Min)

   #    self.message_format(NM_Data,iStep,0)

    @allure.severity('normal')
    @allure.description("Test NMID min can wakeup")
    def test_NMID_range_NMID_Min(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1
      with allure.step(f"Step{iStep}:上电后等待1s,满足radar睡眠条件"):
         time.sleep(1)
         iStep += 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min)
      time.sleep(2)
      iStep = self.RMS_mode_check(Driver,iStep,wakeup_mode=True,i=0,bit=0)

    @allure.severity('normal')
    @allure.description("Test NMID middle can wakeup")
    def test_NMID_range_NMID_Middle1(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1
      with allure.step(f"Step{iStep}:上电后等待1s,满足radar睡眠条件"):
         time.sleep(1)
         iStep += 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      ids = NMID_Min+int(((NMID_Max-NMID_Min)/2))
      iStep = self.passive_wakeup(Driver,iStep,id=ids)
      time.sleep(2)
      iStep = self.RMS_mode_check(Driver,iStep,wakeup_mode=True,i=0)

    @pytest.mark.skip()
    @allure.severity('normal')
    @allure.description("Test NMID middle can wakeup")
    def test_NMID_range_NMID_Middle2(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1
      with allure.step(f"Step{iStep}:上电后等待1s,满足radar睡眠条件"):
         time.sleep(1)
         iStep += 1

         iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
         with assume:assert sleepmode == True
         time.sleep(0.2)

      ids = NMID_Min+int((NMID_Max-NMID_Min)/2)+1
      iStep = self.passive_wakeup(Driver,iStep,id=ids)
      time.sleep(2)
      iStep = self.RMS_mode_check(Driver,iStep,wakeup_mode=True,i=0)

    @allure.severity('normal')
    @allure.description("Test NMID middle can wakeup")
    def test_NMID_range_NMID_Max(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1
      with allure.step(f"Step{iStep}:上电后等待1s,满足radar睡眠条件"):
         time.sleep(1)
         iStep += 1

         iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
         with assume:assert sleepmode == True
         time.sleep(0.2)

      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Max)
      time.sleep(2)
      iStep = self.RMS_mode_check(Driver,iStep,wakeup_mode=True,i=0)

    @allure.severity('normal')
    @allure.description("Test ID less than NMID min can't wakeup")
    def test_NMID_range_less_than_NMID_Min(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1
      with allure.step(f"Step{iStep}:上电后等待1s,满足radar睡眠条件"):
         time.sleep(1)
         iStep += 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      ids = NMID_Min-1
      iStep = self.passive_wakeup(Driver,iStep,id=ids)
      time.sleep(1)
      iStep,sleepmode = self.BSM_mode_check(Driver,iStep,timeout=1)
      with assume:assert sleepmode == True

    @allure.severity('normal')
    @allure.description("Test ID greater than NMID max can't wakeup")
    def test_NMID_range_greater_than_NMID_Max(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1
      with allure.step(f"Step{iStep}:上电后等待1s,满足radar睡眠条件"):
         time.sleep(1)
         iStep += 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      ids = NMID_Max+1
      iStep = self.passive_wakeup(Driver,iStep,id=ids)
      time.sleep(1.6)
      iStep,sleepmode = self.BSM_mode_check(Driver,iStep,timeout=2)
      with assume:assert sleepmode == True

    @allure.severity('normal')
    @allure.description("Test ID greater than NMID max can't wakeup")
    def test_NMID_outofrange_with_Test_frame_APP_message_ID(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1
      with allure.step(f"Step{iStep}:上电后等待1s,满足radar睡眠条件"):
         time.sleep(1)
         iStep += 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=Test_frame_APP_message_ID)
      time.sleep(1.6)
      iStep,sleepmode = self.BSM_mode_check(Driver,iStep,timeout=1)
      with assume:assert sleepmode == True

    @allure.severity('normal')
    @allure.description("Test no message sended under BSM")
    def test_no_message_send_under_BSM(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1
      with allure.step(f"Step{iStep}:上电后等待1s,满足radar睡眠条件"):
         time.sleep(1)
         iStep += 1

         iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
         with assume:assert sleepmode == True

   #  @pytest.mark.repeat(5)
    @allure.description("Test the first frame message after DUT wakes up from BSM is NM message")
    # [TG2_TC2] BSM-RMS
    def test_first_message_after_wakeup_from_BSM_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1
      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.5)
      iStep = self.passive_wakeup(Driver,iStep,id=Test_frame_NM_message_ID,n=5)
      time.sleep(1)
      iStep = self.time_parameters_check(Driver,iStep,True)

   #  @allure.severity('normal')
   #  @allure.description("Test the first frame message after DUT wakes up from BSM is NM message")
   #  # [TG2_TC2] BSM-RMS
   #  def test_first_message_after_wakeup_from_BSM_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    time.sleep(1.5)
   #    iStep = self.time_parameters_check(Driver,iStep,False)
   #    iStep = self.native_wakeup(Driver,False,iStep)
         
    @pytest.mark.skip()
    @allure.severity('normal')
    @allure.description("Test Always send NM message in normal cycle after wakeup")
    def test_always_send_NM_message_in_normal_cycle_after_wakeup_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep)
      time.sleep(2)
      iStep = self.RMS_mode_check(Driver,iStep,True,i=0)

   #  @pytest.mark.skip()
   #  @allure.severity('normal')
   #  @allure.description("Test 10 frames CanNmImmediateNmCycleTimes and then send NM message in normal cycle after wakeup")
   #  def test_10immediateNMCycle_then_send_NM_message_normal_cycle_after_wakeup_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True
   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    time.sleep(0.2)
   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    time.sleep(1.5)
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    time.sleep(0.5)# 需按实际情况调整
   #    iStep = self.RMS_mode_check(Driver,iStep,False,i=0)

    @pytest.mark.skip()
    @allure.severity('normal')
    @allure.description("Test status from BSM to RMS ")
    # [TG2_TC2] BSM-RMS
    def test_status_BSM_switchto_RMS_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep)
      time.sleep(2)
      iStep = self.RMS_mode_check(Driver,iStep,True,i=0)

   #  @pytest.mark.skip()
   #  @allure.severity('normal')
   #  @allure.description("Test status from BSM to RMS ")
   #  # [TG2_TC2] BSM-RMS
   #  def test_status_BSM_switchto_RMS_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    time.sleep(1.5)
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    time.sleep(0.5)# 需按实际情况调整
   #    iStep = self.RMS_mode_check(Driver,iStep,False,i=0)

   #  @pytest.mark.skip()
    @allure.severity('normal')
    @allure.description("Test status from RMS to RSS ")
    # [TG3_TC10] BSM-RMS-RSS
    def test_status_BSM_RMS_RSS_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5)
      time.sleep(2)
      iStep = self.RMS_mode_check(Driver,iStep,True,i=0)
      iStep = self.network_request(Driver,False,iStep)
      iStep = self.RSS_mode_check(Driver,iStep)

   # #  @pytest.mark.skip()
   #  @allure.severity('normal')
   #  @allure.description("Test status from RMS to RSS ")
   #  # [TG3_TC11] BSM-RMS-RSS
   #  def test_status_BSM_RMS_RSS_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    time.sleep(1.5)
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    time.sleep(0.5)  # 需按实际情况调整
   #    iStep = self.RMS_mode_check(Driver,iStep,False,i=0)
   #    iStep = self.RSS_mode_check(Driver,iStep)

   #  @pytest.mark.skip()
    @allure.severity('normal')
    @allure.description("Test status from RMS to NOS ")
    # [TG3_TC8] BSM-RMS-NOS
    def test_status_BSM_RMS_NOS_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5)  # 被动唤醒进入RMS后发送网络请求进入NOS
      time.sleep(1)
      iStep = self.network_request(Driver,True,iStep)
      time.sleep(1)  #等待 repeat message Timer expired
      iStep = self.NOS_mode_check(Driver,iStep)
      iStep = self.network_request(Driver,False,iStep)

   #  @pytest.mark.skip()
   #  @allure.severity('normal')
   #  @allure.description("Test status from RMS to NOS ")
   #  # [TG3_TC9] BSM-RMS-NOS
   #  def test_status_BSM_RMS_NOS_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True
   #    time.sleep(0.2)
   #    iStep = self.native_wakeup(Driver,True,iStep)  # 主动唤醒进入RMS后维持本地唤醒进入NOS
   #    time.sleep(1.5)#等待 repeat message Timer expired
   #    iStep = self.NOS_mode_check(Driver,iStep)
   #    iStep = self.native_wakeup(Driver,False,iStep)

    @allure.severity('normal')
    @allure.description("Test status from NOS to RMS")
    # [TG3_TC12] BSM-RMS-NOS-RMS
    def test_status_BSM_RMS_NOS_RMS_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5)  # 被动唤醒进入RMS后发送网络请求进入NOS
      time.sleep(1)
      iStep = self.network_request(Driver,True,iStep)
      time.sleep(1)  #等待 repeat message Timer expired
      iStep = self.NOS_mode_check(Driver,iStep)
      iStep = self.network_request(Driver,False,iStep)   # Step into RSM

      with allure.step(f"Step{iStep}:发送repeat message request bit报文"):

         for i in range(2):
            time.sleep(0.5)
            passive_frame = Driver['CAN'].send_Frame(NMID_Min,[0x00,0x51,0x00,0x00,0x00,0x00,0x00,0x00],'CAN')
         iStep += 1
      time.sleep(2)

      iStep = self.RMS_mode_check(Driver,iStep,True,i=0)
      

   #  @allure.severity('normal')
   #  @allure.description("Test status from NOS to RMS")
   #  # [TG3_TC13] BSM-RMS-NOS-RMS
   #  def test_status_BSM_RMS_NOS_RMS_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

   #    iStep = self.native_wakeup(Driver,True,iStep)  # 主动唤醒进入RMS后维持本地唤醒进入NOS
   #    time.sleep(1.5)#等待 repeat message Timer expired
   #    iStep = self.NOS_mode_check(Driver,iStep)

   #    with allure.step(f"Step{iStep}:发送repeat message request bit报文"):

   #       for i in range(2):
   #          time.sleep(0.5)
   #          passive_frame = Driver['CAN'].send_Frame(NMID_Min,[0x00,0x51,0x00,0x00,0x00,0x00,0x00,0x00],'CAN')
   #       iStep += 1
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    time.sleep(2)
   #    iStep = self.RMS_mode_check(Driver,iStep,False,i=0)

    @allure.severity('normal')
    @allure.description("Test status from NOS to RSS")
    # [TG3_TC14] BSM-RMS-NOS-RSS
    def test_status_BSM_RMS_NOS_RSS_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5)  # 被动唤醒进入RMS后发生本地唤醒进入NOS
      time.sleep(1)
      iStep = self.network_request(Driver,True,iStep)
      time.sleep(1)  #等待 repeat message Timer expired
      iStep = self.NOS_mode_check(Driver,iStep)
      iStep = self.network_request(Driver,False,iStep)
      time.sleep(1)   #等待network release

      iStep = self.RSS_mode_check(Driver,iStep)

   #  @allure.severity('normal')
   #  @allure.description("Test status from NOS to RSS")
   #  # [TG3_TC15] BSM-RMS-NOS-RSS
   #  def test_status_BSM_RMS_NOS_RSS_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

   #    iStep = self.native_wakeup(Driver,True,iStep)  # 主动唤醒进入RMS后维持本地唤醒进入NOS
   #    time.sleep(1.5)#等待 repeat message Timer expired
   #    iStep = self.NOS_mode_check(Driver,iStep)
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    iStep = self.RSS_mode_check(Driver,iStep)

    @allure.severity('normal')
    @allure.description("Test status from RSS-RMS")
    # [TG3_TC16] BSM-RMS-NOS-RSS-RMS
    def test_status_BSM_RMS_NOS_RSS_RMS_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5)  # 被动唤醒进入RMS后发生本地唤醒进入NOS
      time.sleep(1)
      iStep = self.network_request(Driver,True,iStep)
      time.sleep(1)  #等待 repeat message Timer expired
      # iStep = self.NOS_mode_check(Driver,iStep)
      iStep = self.network_request(Driver,False,iStep)
      time.sleep(1)
      iStep = self.RSS_mode_check(Driver,iStep)
      with allure.step(f"Step{iStep}:清buff"): 
         Driver['CAN'].fush_q_buff()  # 清缓存
         iStep += 1
      # 不确定是不是给这个条件
      with allure.step(f"Step{iStep}:发送repeat message request bit报文"):

         for i in range(3):
            time.sleep(0.5)
            passive_frame = Driver['CAN'].send_Frame(NMID_Min,[0x00,0x51,0x00,0x00,0x00,0x00,0x00,0x00],'CAN')
         iStep += 1
      time.sleep(3)
      iStep = self.RMS_mode_check(Driver,iStep,True,i=0)

   #  @allure.severity('normal')
   #  @allure.description("Test status from RSS-RMS")
   #  # [TG3_TC17] BSM-RMS-NOS-RSS-RMS
   #  def test_status_BSM_RMS_NOS_RSS_RMS_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True
   #    time.sleep(0.2)
   #    iStep = self.native_wakeup(Driver,True,iStep)  # 主动唤醒进入RMS后维持本地唤醒进入NOS
   #    time.sleep(1.5)#等待 repeat message Timer expired
   #    iStep = self.NOS_mode_check(Driver,iStep)
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    iStep = self.RSS_mode_check(Driver,iStep)
   #    with allure.step(f"Step{iStep}:清buff"): 
   #       Driver['CAN'].fush_q_buff()  # 清缓存
   #       iStep += 1
   #    # 不确定是不是给这个条件
   #    with allure.step(f"Step{iStep}:发送repeat message request bit报文"):

   #       for i in range(2):
   #          time.sleep(0.5)
   #          passive_frame = Driver['CAN'].send_Frame(NMID_Min,[0x00,0x51,0x00,0x00,0x00,0x00,0x00,0x00],'CAN')
   #       iStep += 1
   #    time.sleep(2)
   #    iStep = self.RMS_mode_check(Driver,iStep,True,i=0)

    @allure.severity('normal')
    @allure.description("Test status from RSS to NOS")
    # [TG3_TC18] BSM-RMS-NOS-RSS-NOS
    def test_status_BSM_RMS_NOS_RSS_NOS_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5)  # 被动唤醒进入RMS后发生本地唤醒进入NOS
      time.sleep(1)
      iStep = self.network_request(Driver,True,iStep)
      time.sleep(1)  #等待 repeat message Timer expired
      # iStep = self.NOS_mode_check(Driver,iStep)
      iStep = self.network_request(Driver,False,iStep)
      time.sleep(1)
      # iStep = self.RSS_mode_check(Driver,iStep)     #测试case里面有2S的延时，加进去的话会进入PBSM
      iStep = self.network_request(Driver,True,iStep)
      iStep = self.NOS_mode_check(Driver,iStep)
      iStep = self.network_request(Driver,False,iStep)

   #  @allure.severity('normal')
   #  @allure.description("Test status from RSS to NOS")
   #  # [TG3_TC19] BSM-RMS-NOS-RSS-NOS
   #  def test_status_BSM_RMS_NOS_RSS_NOS_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True
   #    time.sleep(0.2)
   #    iStep = self.native_wakeup(Driver,True,iStep)  # 主动唤醒进入RMS后维持本地唤醒进入NOS
   #    time.sleep(1.5)#等待 repeat message Timer expired
   #    iStep = self.NOS_mode_check(Driver,iStep)
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    iStep = self.RSS_mode_check(Driver,iStep)
   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    iStep = self.NOS_mode_check(Driver,iStep)
   #    iStep = self.native_wakeup(Driver,False,iStep)

    @allure.severity('normal')
    @allure.description("Test status from RSS to NOS")
    # [TG3_TC20] BSM-RMS-RSS-NOS
    def test_status_BSM_RMS_RSS_NOS_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5)
      time.sleep(2.5)
      # iStep = self.RSS_mode_check(Driver,iStep)
      iStep = self.network_request(Driver,True,iStep)
      time.sleep(0.2)
      iStep = self.NOS_mode_check(Driver,iStep)
      iStep = self.network_request(Driver,False,iStep)

   #  @allure.severity('normal')
   #  @allure.description("Test status from RSS to NOS")
   #  # [TG3_TC21] BSM-RMS-RSS-NOS
   #  def test_status_BSM_RMS_RSS_NOS_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    time.sleep(1.5)
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    time.sleep(0.5)  # 需按实际情况调整
   #    iStep = self.RSS_mode_check(Driver,iStep)
   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    iStep = self.NOS_mode_check(Driver,iStep)
   #    iStep = self.native_wakeup(Driver,False,iStep)


    @allure.severity('normal')
    @allure.description("Test status from RSS to PBSM")
    # [TG3_TC22] BSM-RMS-RSS-PBSM
    def test_status_BSM_RMS_RSS_PBSM_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,n=2)
      time.sleep(1)
      iStep = self.network_request(Driver,False,iStep,door=True)
      # iStep = self.RSS_mode_check(Driver,iStep) # 因为RSS_mode_check，等待了时间，这个时间可能会影响后面的计时器，所以暂时注释，待考量
      iStep = self.PBSM_mode_check(Driver,iStep)
      time.sleep(2)
      
    @allure.severity('normal')
    @allure.description("Test status from RSS to PBSM")
    # [TG3_TC22] BSM-RMS-RSS-PBSM
    def test_IGN_off_when_door_open(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      Driver['CAN'].set_signal_IGN(0)
      time.sleep(5)
      iStep = self.passive_wakeup(Driver,iStep,n=2)
      time.sleep(1)
      Driver['CAN'].write_db_signal(Set_up.NetName,Set_up.nodeName,'BDC_BCM_2','BCM2_St_RRDoorLock',1)
      # iStep = self.network_request(Driver,False,iStep,door=True)
      # iStep = self.RSS_mode_check(Driver,iStep) # 因为RSS_mode_check，等待了时间，这个时间可能会影响后面的计时器，所以暂时注释，待考量
      iStep = self.PBSM_mode_check(Driver,iStep)
      time.sleep(2)

    @pytest.mark.skip()
    @allure.severity('normal')
    @allure.description("Test status from RSS to PBSM")
    # [TG3_TC23] BSM-RMS-RSS-PBSM
    def test_status_BSM_RMS_RSS_PBSM_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.native_wakeup(Driver,True,iStep)
      time.sleep(1.5)
      iStep = self.native_wakeup(Driver,False,iStep)
      time.sleep(0.5)  # 需按实际情况调整
      # iStep = self.RSS_mode_check(Driver,iStep)
      iStep = self.PBSM_mode_check(Driver,iStep)

    @allure.severity('normal')
    @allure.description("Test status from RSS-PBSM")
    # [TG3_TC24] BSM-RMS-NOS-RSS-PBSM
    def test_status_BSM_RMS_NOS_RSS_PBSM_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5)  # 被动唤醒进入RMS后发生本地唤醒进入NOS
      time.sleep(0.5)
      iStep = self.network_request(Driver,True,iStep)
      time.sleep(1.2)  #等待 repeat message Timer expired
      iStep = self.NOS_mode_check(Driver,iStep)
      iStep = self.network_request(Driver,False,iStep)
      time.sleep(1)

      iStep = self.RSS_mode_check(Driver,iStep)  # 因为RSS_mode_check，等待了时间，这个时间可能会影响后面的计时器，所以暂时注释，待考量
      iStep = self.PBSM_mode_check(Driver,iStep)      # 包含NM-Timeout Timer expired

   #  @allure.severity('normal')
   #  @allure.description("Test status from RSS-PBSM")
   #  # [TG3_TC25] BSM-RMS-NOS-RSS-PBSM
   #  def test_status_BSM_RMS_NOS_RSS_PBSM_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

   #    iStep = self.native_wakeup(Driver,True,iStep)  # 主动唤醒进入RMS后维持本地唤醒进入NOS
   #    time.sleep(1.5)#等待 repeat message Timer expired
   #    iStep = self.NOS_mode_check(Driver,iStep)
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    # iStep = self.RSS_mode_check(Driver,iStep)  # 因为RSS_mode_check，等待了时间，这个时间可能会影响后面的计时器，所以暂时注释，待考量
   #    iStep = self.PBSM_mode_check(Driver,iStep)

    @allure.severity('normal')
    @allure.description("Test status from PBSM-RMS")
    # [TG3_TC26] BSM-RMS-RSS-PBSM-RMS
    def test_status_BSM_RMS_RSS_PBSM_RMS_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True

      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5) 
      time.sleep(1.5)
      # iStep = self.RSS_mode_check(Driver,iStep) # 因为RSS_mode_check，等待了时间，这个时间可能会影响后面的计时器，所以暂时注释，待考量
      Driver['CAN'].set_signal_IGN(0)
      time.sleep(3)
      iStep = self.PBSM_mode_check(Driver,iStep)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5) 
      time.sleep(2)
      iStep = self.RMS_mode_check(Driver,iStep,wakeup_mode=True,i=0)
      

   #  @allure.severity('normal')
   #  @allure.description("Test status from PBSM-RMS")
   #  # [TG3_TC27] BSM-RMS-RSS-PBSM-RMS
   #  def test_status_BSM_RMS_RSS_PBSM_RMS_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    time.sleep(1.5)
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    time.sleep(0.5)  # 需按实际情况调整
   #    # iStep = self.RSS_mode_check(Driver,iStep)
   #    iStep = self.PBSM_mode_check(Driver,iStep)
   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    time.sleep(2)
   #    iStep = self.RMS_mode_check(Driver,iStep,wakeup_mode=False,i=0)
   #    iStep = self.native_wakeup(Driver,False,iStep)

    @pytest.mark.skip()
    @allure.severity('normal')
    @allure.description("Test status from PBSM-BSM")
    # [TG3_TC28] BSM-RMS-RSS-PBSM-BSM
    def test_status_BSM_RMS_RSS_PBSM_BSM_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5) 
      time.sleep(1.5)
      # iStep = self.RSS_mode_check(Driver,iStep) # 因为RSS_mode_check，等待了时间，这个时间可能会影响后面的计时器，所以暂时注释，待考量
      iStep = self.PBSM_mode_check(Driver,iStep)
      time.sleep(2)
      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      
   #  @pytest.mark.skip()
   #  @allure.severity('normal')
   #  @allure.description("Test status from PBSM-BSM")
   #  # [TG3_TC29] BSM-RMS-RSS-PBSM-BSM
   #  def test_status_BSM_RMS_RSS_PBSM_BSM_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    time.sleep(1.5)
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    time.sleep(0.5)  # 需按实际情况调整
   #    # iStep = self.RSS_mode_check(Driver,iStep)
   #    iStep = self.PBSM_mode_check(Driver,iStep)
   #    time.sleep(2)
   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

    @allure.severity('normal')
    @allure.description("Test status from BSM-RMS")
    # [TG3_TC28] BSM-RMS-RSS-PBSM-BSM-RMS
    def test_status_BSM_RMS_RSS_PBSM_BSM_RMS_passive_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5) 
      time.sleep(1.5)
      # iStep = self.RSS_mode_check(Driver,iStep) # 因为RSS_mode_check，等待了时间，这个时间可能会影响后面的计时器，所以暂时注释，待考量
      iStep = self.PBSM_mode_check(Driver,iStep)
      time.sleep(2)
      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      iStep = self.passive_wakeup(Driver,iStep,id=NMID_Min,n=5) 
      time.sleep(2)
      iStep = self.RMS_mode_check(Driver,iStep,wakeup_mode=True,i=0)
      
   #  @allure.severity('normal')
   #  @allure.description("Test status from BSM-RMS")
   #  # [TG3_TC29] BSM-RMS-RSS-PBSM-BSM-RMS
   #  def test_status_BSM_RMS_RSS_PBSM_BSM_RMS_native_wakeup(self,env_initial,action,Driver,power_reset,sleep_mode):
   #    iStep = 1

   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True

   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    time.sleep(1.5)
   #    iStep = self.native_wakeup(Driver,False,iStep)
   #    time.sleep(0.5)  # 需按实际情况调整
   #    # iStep = self.RSS_mode_check(Driver,iStep)
   #    iStep = self.PBSM_mode_check(Driver,iStep)
   #    time.sleep(2)
   #    iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
   #    with assume:assert sleepmode == True
   #    iStep = self.native_wakeup(Driver,True,iStep)
   #    time.sleep(2)
   #    iStep = self.RMS_mode_check(Driver,iStep,wakeup_mode=True,i=0)    
   #    iStep = self.native_wakeup(Driver,False,iStep)
      
      

    def get_0x386_ADAS_signal(self,Driver):  
      RCTB_0x386 = Driver['CAN'].read_db_signal(Set_up.NetName,Set_up.nodeName,'CMRR_RR','CMRR_RR2_SwFb_RCTB')
      RCW_0x386 = Driver['CAN'].read_db_signal( Set_up.NetName,Set_up.nodeName,'CMRR_RR','CMRR_RR2_SwFb_RCW')
      RCTA_0x386 = Driver['CAN'].read_db_signal( Set_up.NetName,Set_up.nodeName,'CMRR_RR','CMRR_RR2_SwFb_RCTA')
      DOW_0x386 = Driver['CAN'].read_db_signal( Set_up.NetName,Set_up.nodeName,'CMRR_RR','CMRR_RR2_SwFb_DOW')
      BSD_0x386 = Driver['CAN'].read_db_signal( Set_up.NetName,Set_up.nodeName,'CMRR_RR','CMRR_RR2_SwFb_BSD_LCA')
      return RCTB_0x386,RCW_0x386,RCTA_0x386,DOW_0x386,BSD_0x386

    @pytest.mark.skip
    @allure.severity('normal')
    @allure.description("Test status from RSS-PBSM")
    # [TG3_TC24] BSM-RMS-NOS-RSS-PBSM
    def test_ADAS_RCTB_RCW_RCTA_DOW_BSD_button(self,env_initial,action,Driver,power_reset,sleep_mode):
      iStep = 1

      iStep,sleepmode = self.BSM_mode_check(Driver,iStep)
      with assume:assert sleepmode == True
      time.sleep(0.2)
      passive_frame = Driver['CAN'].send_CANFrame_Cycle(0x400,[0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00],500,'CAN')
      time.sleep(0.5)
      iStep = self.network_request(Driver,True,iStep)
      # time.sleep(1.2)  #等待 repeat message Timer expired

      Driver['CAN'].set_signal_RCTB_RCW(1,1)
      RCTA_0x523 = Driver['CAN'].send_Frame(0x523,[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01],'CANFD')    # on
      BSD_DOW_0xED = Driver['CAN'].send_Frame(0xED,[0x00,0x00,0x14,0x00,0x00,0x00,0x00,0x00],'CANFD')    # on
      time.sleep(0.2)

      RCTB_0x386,RCW_0x386,RCTA_0x386,DOW_0x386,BSD_0x386 = self.get_0x386_ADAS_signal(Driver)
      logging.info(f'车身功能开关ON,RCTB_0x386 功能开关状态实际:{RCTB_0x386},期望为3;  RCW_0x386 功能开关状态实际:{RCW_0x386},期望为1;RCTA_0x386 功能开关状态实际:{RCTA_0x386},期望为3; ')
      logging.info(f'车身功能开关ON,DOW_0x386 功能开关状态实际:{DOW_0x386},期望为3;  BSD_0x386 功能开关状态实际:{BSD_0x386},期望为3 ')
      with assume:assert RCTB_0x386 == RCTA_0x386 == DOW_0x386 == BSD_0x386 == 3
      with assume:assert RCW_0x386 == 1


      Driver['CAN'].stop_FrANFrame_Cycle(passive_frame)
      iStep = self.network_request(Driver,False,iStep)
      time.sleep(2)

      passive_frame = Driver['CAN'].send_CANFrame_Cycle(0x400,[0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00],500,'CAN')# 被动唤醒进入RMS后发生本地唤醒进入NOS
      time.sleep(0.5)
      iStep = self.network_request(Driver,True,iStep)

      RCTB_0x386,RCW_0x386,RCTA_0x386,DOW_0x386,BSD_0x386 = self.get_0x386_ADAS_signal(Driver)
      logging.info(f'车身功能开关ON,RCTB_0x386 功能开关状态实际:{RCTB_0x386},期望为3;  RCW_0x386 功能开关状态实际:{RCW_0x386},期望为1;RCTA_0x386 功能开关状态实际:{RCTA_0x386},期望为3; ')
      logging.info(f'车身功能开关ON,DOW_0x386 功能开关状态实际:{DOW_0x386},期望为3;  BSD_0x386 功能开关状态实际:{BSD_0x386},期望为3 ')
      with assume:assert RCTB_0x386 == RCTA_0x386 == DOW_0x386 == BSD_0x386 == 3
      with assume:assert RCW_0x386 == 1


      Driver['CAN'].set_signal_RCTB_RCW(2,2)
      RCTA_0x523 = Driver['CAN'].send_Frame(0x523,[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02],'CANFD')  # off
      BSD_DOW_0xED = Driver['CAN'].send_Frame(0xED,[0x00,0x00,0x28,0x00,0x00,0x00,0x00,0x00],'CANFD')    # oFF
      time.sleep(0.3)

      RCTB_0x386,RCW_0x386,RCTA_0x386,DOW_0x386,BSD_0x386 = self.get_0x386_ADAS_signal(Driver)
      logging.info(f'车身功能开关OFF,RCTB_0x386 功能开关状态实际:{RCTB_0x386},期望为0 ;  RCW_0x386 功能开关状态实际:{RCW_0x386},期望为0 ;RCTA_0x386 功能开关状态实际:{RCTA_0x386},期望为0 ; ')
      logging.info(f'车身功能开关oFF,DOW_0x386 功能开关状态实际:{DOW_0x386},期望为0;  BSD_0x386 功能开关状态实际:{BSD_0x386},期望为0 ')
      with assume:assert RCTB_0x386 == RCW_0x386 == RCTA_0x386 == DOW_0x386 == BSD_0x386  == 0

      Driver['CAN'].set_signal_RCTB_RCW(3,3)
      RCTA_0x523 = Driver['CAN'].send_Frame(0x523,[0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03],'CANFD')  # invalid
      BSD_DOW_0xED = Driver['CAN'].send_Frame(0xED,[0x00,0x00,0x3C,0x00,0x00,0x00,0x00,0x00],'CANFD')    # invalid
      time.sleep(0.2)

      RCTB_0x386,RCW_0x386,RCTA_0x386,DOW_0x386,BSD_0x386 = self.get_0x386_ADAS_signal(Driver)
      logging.info(f'车身功能开关invalid,RCTB_0x386 功能开关状态实际:{RCTB_0x386},期望为0 ;  RCW_0x386 功能开关状态实际:{RCW_0x386},期望为0 ;RCTA_0x386 功能开关状态实际:{RCTA_0x386},期望为0 ; ')
      logging.info(f'车身功能开关invalid,DOW_0x386 功能开关状态实际:{DOW_0x386},期望为0;  BSD_0x386 功能开关状态实际:{BSD_0x386},期望为0 ')
      with assume:assert RCTB_0x386 == RCW_0x386 == RCTA_0x386 == DOW_0x386 == BSD_0x386  == 0


      Driver['CAN'].stop_FrANFrame_Cycle(passive_frame)
      iStep = self.network_request(Driver,False,iStep)
      time.sleep(2)
      Driver['CAN'].restartMRR_Power(Driver)
      passive_frame = Driver['CAN'].send_CANFrame_Cycle(0x400,[0x00,0x40,0x00,0x00,0x00,0x00,0x00,0x00],500,'CAN')# 被动唤醒进入RMS后发生本地唤醒进入NOS
      time.sleep(0.5)
      iStep = self.network_request(Driver,True,iStep)

      RCTB_0x386,RCW_0x386,RCTA_0x386,DOW_0x386,BSD_0x386 = self.get_0x386_ADAS_signal(Driver)
      logging.info(f'休眠后RCTB_0x386 功能开关状态实际:{RCTB_0x386},期望为0 ;  休眠后RCW_0x386 功能开关状态实际:{RCW_0x386},期望为0;休眠后RCTA_0x386 功能开关状态实际:{RCTA_0x386},期望为0 ; ')
      logging.info(f'休眠后车身功能开关oFF,DOW_0x386 功能开关状态实际:{DOW_0x386},期望为0;  休眠后BSD_0x386 功能开关状态实际:{BSD_0x386},期望为0 ')
      with assume:assert RCTB_0x386 == RCW_0x386 == RCTA_0x386 == DOW_0x386 == BSD_0x386  == 0
      Driver['CAN'].stop_FrANFrame_Cycle(passive_frame)
      iStep = self.network_request(Driver,False,iStep)


